package cate.game.stat.item;

import cate.common.table.d.GDObj;
import cate.game.GameBody;
import cate.game.framework.ModuleMan;
import cp.solution.business.stat.item.MoneyIoEntity;
import cp.solution.rpc.path.RpcStatPath;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;
import java.util.Date;

@Slf4j
public class StatMoneyHandler extends ModuleMan {

    private int lastDay;

    /** 金币 */
    private MoneyIoEntity data0 = new MoneyIoEntity();
    /** 碎钻 */
    private MoneyIoEntity data1 = new MoneyIoEntity();
    /** 钻石 */
    private MoneyIoEntity data88 = new MoneyIoEntity();

    @Override
    public void initDependent() throws Exception {
        Date now = new Date();

        data0.setSrvId(game.config.srvId);
        data0.setGroupCode(game.config.gs.getGroupCode());
        data0.setDate(now);
        data0.setType(GDObj.Num.M0);

        data1.setSrvId(game.config.srvId);
        data1.setGroupCode(game.config.gs.getGroupCode());
        data1.setDate(now);
        data1.setType(GDObj.Num.M1);

        data88.setSrvId(game.config.srvId);
        data88.setGroupCode(game.config.gs.getGroupCode());
        data88.setDate(now);
        data88.setType(GDObj.Num.DIAMAND);
    }

    private void clear() {
        Date now = new Date();
        data0.setDate(now);
        data1.setDate(now);
        data88.setDate(now);
        data0.setNumAdd(0);
        data1.setNumAdd(0);
        data88.setNumAdd(0);
        data0.setNumReduce(0);
        data1.setNumReduce(0);
        data88.setNumReduce(0);
    }

    public void recordItem(StatItemIOEntity item){
        if (item.getItemNum() == 0) {
            return;
        }
        if(item.getItemType() != GDObj.Type.NUM) {
            return;
        }
        int curDay = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        if(curDay != lastDay) {
            lastDay = curDay;
            upload(game, data0.copy(), data1.copy(), data88.copy());
            clear();
        }
        if(item.getItemTid() == GDObj.Num.M0) {
            data0.accumulate(item.getItemNum());
        } else if(item.getItemTid() == GDObj.Num.M1) {
            data1.accumulate(item.getItemNum());
        } else if(item.getItemTid() == GDObj.Num.DIAMAND) {
            data88.accumulate(item.getItemNum());
        }
    }

    private static boolean hasVal(MoneyIoEntity d) {
        return d.getNumAdd() != 0 || d.getNumReduce() != 0;
    }

    private static void upload(GameBody game, MoneyIoEntity d0, MoneyIoEntity d1, MoneyIoEntity d88) {
        try {
            if (hasVal(d0)) {
                Integer n0 = game.rpcSend().requestResponseStat(RpcStatPath.MONEY_IO, d0, Integer.class);
                log.info("货币IO统计上报：n0={}", n0);
            }
            if (hasVal(d1)) {
                Integer n1 = game.rpcSend().requestResponseStat(RpcStatPath.MONEY_IO, d1, Integer.class);
                log.info("货币IO统计上报：n1={}", n1);
            }
            if (hasVal(d88)) {
                Integer n88 = game.rpcSend().requestResponseStat(RpcStatPath.MONEY_IO, d88, Integer.class);
                log.info("货币IO统计上报：n88={}", n88);
            }
        } catch (Exception e) {
            log.error("货币IO统计上报异常", e);
        }
    }

    public void every3Min() {
        upload(game, data0, data1, data88);
        clear();
    }

}
